package cn.itcast.service.system.impl;

import cn.itcast.dao.system.RoleDao;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    //分页查询角色
    public PageInfo findAll(String loginCompanyId, int page, int size) {
        PageHelper.startPage(page,size);
        List<Role> list = roleDao.findAll(loginCompanyId);
        return new PageInfo(list);
    }

    //保存
    public void save(Role role) {
        role.setId(UUID.randomUUID().toString());
        roleDao.save(role);
    }

    //更新
    public void update(Role role) {
        roleDao.update(role);
    }

    //根据id查询
    public Role findById(String id) {
        return roleDao.findById(id);
    }

    @Override
    public void delete(String id) {
        roleDao.delete(id);
    }

    @Override
    public List<Role> findAll(String CompanyId) {
        return roleDao.findAll(CompanyId);
    }

    //根据id查询，查询此角色具有的所有模块id
    public List<String> findModulesByRoleId(String roleId) {
        return roleDao.findModulesByRoleId(roleId);
    }

    //实现对角色分配权限
    public void updateRoleModule(String roleid, String moduleIds) {
        //调用dao根据角色id删除所有角色id中间表数据
        roleDao.deleteRoleModule(roleid);
        //将moduleIds拆分为模块id数组
        String[] mids = moduleIds.split(",");
        //循环所有模块id数组
        for (String moduleId : mids) {
            //调用dao向角色模块中间表保存数据
            roleDao.saveRoleModule(roleid,moduleId);
        }
    }


}
